﻿using System;
using System.Windows;
using System.Windows.Controls;
using WiseLinkWPF.Windows.Controls;
using WiseLinkWPF.Windows.Controls.GridView;
using WiseLinkWPF.Windows.Data;

namespace CCF20Module
{
    /// <summary>
    /// Interaction logic for YesNoFilter.xaml
    /// </summary>
    public partial class YesNoFilter : UserControl, IFilteringControl
    {
        private GridViewBoundColumnBase column;
        private CompositeFilterDescriptor compositeFilter;
        private WiseLinkWPF.Windows.Data.FilterDescriptor yesNoFilter;
        private WiseLinkWPF.Windows.Data.FilterDescriptor nullFilter;
        public YesNoFilter()
        {
            InitializeComponent();
        }
        
        public static readonly DependencyProperty IsActiveProperty =
           DependencyProperty.Register(
               "IsActive",
               typeof(bool),
               typeof(YesNoFilter),
               new System.Windows.PropertyMetadata(false));

        public bool IsActive
        {
            get { return (bool)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }

        public void Prepare(GridViewBoundColumnBase column)
        {
            this.column = column;

            if (this.compositeFilter == null)
            {
                this.CreateFilters();
            }
        }
        private void CreateFilters()
        {

            string dataMember = this.column.DataMemberBinding.Path.Path;
            this.yesNoFilter = new WiseLinkWPF.Windows.Data.FilterDescriptor(dataMember
                                                            , WiseLinkWPF.Windows.Data.FilterOperator.IsEqualTo
                                                            , null);
            this.nullFilter = new WiseLinkWPF.Windows.Data.FilterDescriptor(dataMember
                                                            , WiseLinkWPF.Windows.Data.FilterOperator.IsEqualTo
                                                            , null);

            this.compositeFilter = new CompositeFilterDescriptor();
            
            

        }
        private void OnFilter(object sender, RoutedEventArgs e)
        {
            CleanUp();
            
            this.yesNoFilter.Value = this.btnYes.IsChecked == true ? true : false;
            this.compositeFilter.FilterDescriptors.Add(this.yesNoFilter);

            if (this.btnNo.IsChecked == true)
            {
                compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
                this.compositeFilter.FilterDescriptors.Add(this.nullFilter);
            }

            if (!this.column.DataControl.FilterDescriptors.Contains(this.compositeFilter))
            {
                this.column.DataControl.FilterDescriptors.Add(this.compositeFilter);
            }

            this.IsActive = true;
        }

        private void OnClear(object sender, RoutedEventArgs e)
        {
            CleanUp();
            this.IsActive = false;

        }
        private void CleanUp()
        {
            if (this.column.DataControl.FilterDescriptors.Contains(this.compositeFilter))
            {
                this.column.DataControl.FilterDescriptors.Remove(this.compositeFilter);
            }
            this.compositeFilter.FilterDescriptors.Clear();
        }
    }
}
